<template>
	<view>
		<uv-navbar title="手机号登录" @leftClick="leftClick"></uv-navbar>
	</view>
	<view class="content">
		<image class="logo" src="/static/logo.png"></image>
		<view class="text-area">
			<input type="text" v-model="phoneNumber" placeholder="请输入手机号" />
			<input type="text" v-model="verificationCode" placeholder="请输入验证码" />
		</view>
		<view class="btn">
			<button :disabled="!isValidPhone || !checked" :style="{  backgroundColor: (isValidPhone && checked) ? '#F59A23' : '#D7D7D7',
			    color: '#FFF'}" @click="sendVerificationCode">获取验证码</button>
			<button @click="loginWithPhone"><text>登录</text></button>
		</view>
		<view class="term" @click="toggleCheck">
			<label class="radio">
				<radio value="r1" :checked="checked" />
				<text>同意《中国移动认证服务条款》《用户协议》</text>
			</label>
		</view>
		<view class="bottom">
			<uv-icon size="32" color="#50b674" name="weixin-circle-fill"></uv-icon>
		</view>
	</view>
</template>

<script>
	import axios from 'axios';

	export default {
		data() {
			return {
				phoneNumber: '',
				verificationCode: '',
				checked: false
			}
		},
		computed: {
			isValidPhone() {
				return /^1[3-9]\d{9}$/.test(this.phoneNumber)
			}
		},
		methods: {
			leftClick() {
				uni.navigateBack()
			},
			toggleCheck() {
				this.checked = !this.checked
				console.log('checked:', this.checked); // 添加日志输出
			},
			async sendVerificationCode() {
				try {
					const response = await axios.post('http://localhost:3000/api/auth/send-code', {
						phoneNumber: this.phoneNumber
					});
					console.log('验证码发送成功:', response.data);
				} catch (error) {
					console.error('验证码发送失败:', error);
				}
			},
			async loginWithPhone() {
				try {
					const response = await axios.post('http://localhost:3000/api/auth/verify-code', {
						phoneNumber: this.phoneNumber,
						code: this.verificationCode
					});
					console.log('登录成功:', response.data);
					uni.switchTab({
						url:'/pages/bookcity/selectedView'
					})
				} catch (error) {
					console.error('登录失败:', error);
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
		flex-direction: column;
		align-items: center;
	}

	.text-area input {
		font-size: 15px;
		width: 300px;
		border-bottom: 1px solid black;
		padding-bottom: 15px;
	}

	.btn {
		padding-top: 20px;
	}

	.btn button {
		width: 300px;
		height: 47px;
		font-size: 14px;
		border-radius: 39px;
		background-color: #F59A23;
		margin-bottom: 20rpx;

		display: flex;
		justify-content: center;
		align-items: center;

	}

	.btn text {
		color: #fff;
		line-height: 47px;
	}

	.term {
		padding-top: 20px;
	}

	.term text {
		color: #F59A23;
	}

	.bottom {
		width: 100%;
		display: flex;
		justify-content: center;
		bottom: 60px;
		position: absolute;
	}
</style>